$(function() {

    //begin列表信息
    $("#attence_datagrid").datagrid({
        url: "/attence/list",
        columns: [[
            {field: "xxx", checkbox: 'true'},
            {field: "id", title: "id", width: 100,hidden:true},
            {field: "sn", title: "考勤编号", width: 100},
            {field: "employee", title: "姓名", width: 100,formatter:function (value,row,index){
                if(value){
                    return value.username;
                }
                return "";
            }},
            {field: "intime", title: "签到时间", width: 100},
            {field: "outtime", title: "签退时间", width: 100},
            {field: "intState", title: "签到状态", width: 100,formatter:function (value,row,index){
                if(value == 1){
                    return "<font face='楷体' color='green'>正常</font>";
                }else if(value == 0) {
                    return "<font face='楷体' color='red'>迟到</font>";
                }else {
                    return "<font face='楷体' color='red'>考勤异常</font>";
                }
            }},
            {
                field: "outState", title: "签退状态", width: 100, formatter: function (value, row, index) {
                if (value == 1) {
                    return "<font face='楷体' color='green'>正常</font>";
                } else if (value == 0) {
                    return "<font face='楷体' color='red'>早退</font>";
                } else {
                    return "<font face='楷体' color='red'>考勤异常</font>";
                }
            }
            }
        ]],
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        striped: true,
        pageNumber:1,
        pageSize:3,
        pageList:[3,5,10,20],
        toolbar: "#attence_tb"
    });
    //end列表信息

    //begin添加/编辑对话框
    $("#attence_dialog").dialog({
        width: 500,
        height: 500,
        buttons: "#attence_bb",
        closed: true
    });
    //end添加/编辑对话框

    //begin处理功能方法的对象
    var obj = {
        //begin添加按钮
        add: function () {
            $("#attence_form").form("clear");
            $("#attence_dialog").dialog("setTitle", "考勤添加");
            $("#attence_dialog").dialog("open");
        },
        //end添加按钮

        //begin编辑按钮
        edit: function () {
            $("#attence_form").form("clear");
            $("#attence_dialog").dialog("setTitle", "考勤编辑");
            var row = $("#attence_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            row["employee.username"] = row.employee.username;
            row["employee.id"] = row.employee.id;
            $("#attence_form").form("load", row);
            $("#attence_dialog").dialog("open");
        },
        //end编辑按钮

        //begin取消按钮
        cancel: function () {
            $("#attence_dialog").dialog("close");
        },
        //end取消按钮

        //begin保存按钮
        save: function () {
            var attenceId = $("#attenceId").val();
            var url;
            if (attenceId) {
                url = "/attence/update"
            } else {
                url = "/attence/save"
            }
            $("#attence_form").form("submit", {
                url: url,
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        $.messager.alert('温馨提示', "操作成功!");
                        $("#attence_dialog").dialog("close");
                        $("#attence_datagrid").datagrid("reload");
                    }
                }
            });
        },
        //end保存按钮

        //begin删除按钮
        remove:function(){
            var row = $("#attence_datagrid").datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', '请选择要操作的行!');
                return;
            }
            $.messager.confirm('确认','您确认要操作此行吗？',function(r){
                if (r){
                    //注意这里的id要从当前选中的行中获取
                    var attenceId = row.id;
                    $.get("/attence/delete",{attenceId:attenceId},function(data){
                        if(!data.success){
                            $.messager.alert('温馨提示', data.msg);
                        }else{
                            $.messager.alert('温馨提示', '操作成功!');
                            $("#attence_datagrid").datagrid("reload");
                        }
                    });
                }
            });

        },
        //end删除按钮

        //begin查询按钮
        search: function () {
            $("#attence_datagrid").datagrid("load",{
                month:$("#month").textbox("getValue")
            });
        },
        //end查询按钮

        //begin刷新按钮
        reload:function (){
            $("#attence_datagrid").datagrid("reload")
        }
        //end刷新按钮
    };
    //end处理功能方法的对象

    //begin处理功能方法对象,实际调用方法
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        obj[cmd]();
    });
    //end处理功能方法对象,实际调用方法

});